<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="所属会议" prop="project_id">
                        <el-input v-model="project_code" disabled placeholder="请选择"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="会议名称" prop="project_name">
                        <el-input v-model="project_name" disabled placeholder="会议名称"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="项目日期" prop="project_time">
                        <el-input v-model="project_time" disabled placeholder="项目日期"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="商品名称" prop="goods_id">
                        <el-input v-model="goods_name" placeholder="商品名称" readonly @click="checkProduct"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="12">
                <el-form-item label="明细" prop="detail">
                    <el-input v-model="form.detail" placeholder="明细" type="textarea"></el-input>
                </el-form-item>
            </el-col>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="单价" prop="price">
                        <el-input v-model="form.price" placeholder="单价" @input="totalCalc"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="数量" prop="number">
                        <el-input v-model="form.number" placeholder="数量" @input="totalCalc"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="12">
                <el-form-item label="小计" prop="total">
                    <el-input v-model="total" placeholder="小计" readonly></el-input>
                </el-form-item>
            </el-col>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="结算周期" prop="settlement">
                        <el-select v-model="form.settlement" placeholder="请选择结算周期" style="width:100%" clearable filterable>
                            <el-option label="月结" :value="0"/>
                            <el-option label="现结" :value="1"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="是否结账" prop="is_checkout">
                        <el-select v-model="form.is_checkout" placeholder="请选择" style="width:100%" clearable filterable>
                            <el-option label="是" :value="1"/>
                            <el-option label="否" :value="0"/>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="供应商名称" prop="supplier_id">
                        <el-input v-model="supplier_name" readonly placeholder="请选择" @click="checkApply"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12" v-if="mode=='edit'">
                    <el-form-item label="关联备用金" prop="reserve_fund_id">
                        <el-input v-model="reserve_fund_name" readonly placeholder="请选择"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <item-list v-if="dialog.item" ref="itemDialog" @success="itemBack" @closed="dialog.item=false"></item-list>
    <select-apply v-if="dialog.apply" ref="applyDialog" @success="applyBack" @closed="dialog.apply=false"></select-apply>
    <product-list v-if="dialog.product" ref="productDialog" @success="productBack" @closed="dialog.product=false"></product-list>
</template>

<script>
import SelectApply from '@/views/supplier/list/selectApply.vue';
import itemList from '../list/itemList.vue';
import ProductList from '../projectcost/productList.vue';
export default {
    components: { itemList, SelectApply,ProductList},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
                project_id:"",
                goods_id:"",
                detail:"",
                price:"",
                number:"",
                settlement:"",
                is_checkout:"",
                supplier_id:"",
                reserve_fund_id:"",
			},
            project_code: "",
            project_name: "",
            project_time: "",
            supplier_name:"",
            goods_name:"",
            total:"",
            reserve_fund_name:"",
			//验证规则
			rules: {
				project_id: [{ required: true, message: '请选择项目' }],
			},
            dialog:{
                item:false,
                apply:false,
                product:false,
            },
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.project.itemcost.edit.post(this.form);
					} else {
						res = await this.$API.project.itemcost.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
        setDataAdd(data){
            this.form.project_id = data.project_id
            this.project_code = data.project_code
            this.project_name = data.project_name
            this.project_time = data.project_time
            this.form.reserve_fund_id = data.reserve_fund_id
        },
		//表单注入数据
		setData(data) {
            this.form.id = data.id
            this.form.goods_id = data.goods_id
            this.form.detail = data.detail
            this.form.price = data.price
            this.form.number = data.number
            this.form.settlement = data.settlement
            this.form.is_checkout = data.is_checkout
            this.form.supplier_id = data.supplier_id
            this.project_code = data.project.project_code
            this.project_name = data.project.metting
            this.project_time = data.project.project_time
            this.supplier_name = data.supplier.supplier_name
            this.goods_name = data.goods.goods_name
            this.total = Number(data.price) * Number(data.number)
            this.form.reserve_fund_id = data.reserve!=null?data.reserve.id:""
            this.reserve_fund_name = data.reserve_code
		},
        // 项目
        checkItem(){
            this.dialog.item = true
			this.$nextTick(() => {
				this.$refs.itemDialog.open('add')
			})
        },
        itemBack(data){
            this.form.project_id = data.id
            this.project_code = data.project_code
            this.project_name = data.metting
            this.project_time = data.project_time
        },
        // 供应商
        checkApply(){
            this.dialog.apply = true
			this.$nextTick(() => {
				this.$refs.applyDialog.open('add')
			})
        },
        applyBack(data){
            this.form.supplier_id = data.id
            this.supplier_name = data.supplier_name
        },
        // 商品
        checkProduct(){
            this.dialog.product = true
			this.$nextTick(() => {
				this.$refs.productDialog.open('add')
			})
        },
        productBack(data){
            this.form.goods_id = data.id
            this.goods_name = data.goods_name
        },
        totalCalc(){
            if(this.form.price && this.form.number){
                this.total = Number(this.form.price) * Number(this.form.number)
            }
        },
	}
}
</script>

<style></style>
